/* ************************************************************************

   Copyright:
     2008 openWorks

   License:
     LGPL: http://www.gnu.org/licenses/lgpl.html
     See the LICENSE file in the project's top-level directory for details.

   Authors:
     * Juan Matias Granda
     * Martín Alejandro Paz

************************************************************************ */

/**
* Objeto creado con dos {@link qx.ui.basic.Label} y dos {@link  qx.ui.form.List} y dos {@link  qx.ui.form.Button}.
* 
* *Ejemplo:*
*
* <pre class='javascript'>
*  var datosEstructura = 
*  [
*   {value: '1' , label: 'nombre del item1', selected: true},  
*   {label: 'nombre del item 2', value: '2'},
*   {label: 'nombre del item 4'},
*   {value: '5', selected: true}
*  ]
*   var swEjemplo = new qx.ui.form.ow.ListSwitch("List No Selec.","List Seleccionados", datosEstructura)
*  </pre>
*/
qx.Class.define("qx.ui.form.ow.ListSwitch",
{
	extend : qx.ui.container.Composite,
	/**
	* @param labelLeft {String} Titulo que acompaña al List de los objetos no seleccionados.
	* @param labelRight {String} Titulo que acompaña al List de los objetos seleccionados.
	* @param  structure {Map} Array de datos que se pasan al Constructor. <br />
	* *Parametros:*
	* <ul>
	* <li>*label:* Texto a mostrar en el item.</li>
	* <li>*value:* Valor del item.</li>
	* <li>*selected:* _(Default: false)_ Si el objeto estara seleccionado en el inicio.</li>
	* </ul>
	* _Aclaracion:_ Al menos debe existir _label_ o _value_, el cual cumplira la funcion de ambos.
	*/
	construct : function (labelLeft, labelRight, structure){
		this.base(arguments);
		var layer = new qx.ui.layout.Grid(5,5);
			layer.setRowAlign(0, "center", "middle");
			layer.setRowAlign(1, "center", "middle");
		this.setLayout(layer);;
			this._listLeft = new qx.ui.form.List();
				this._listLeft.setHeight(120);
			this._listRight = new qx.ui.form.List();
			this._listRight.setHeight(120);
			this._btnToRight = new qx.ui.form.Button(">>");
			this._btnToLeft = new qx.ui.form.Button("<<");
					
			for(r in structure){
				if (structure[r].selected) {
					if (structure[r].value != null)
					{
						if (structure[r].label != null)
						{
							this._listRight.add(new qx.ui.form.ListItem(structure[r].label, structure[r].icon, structure[r].value));
						}
						else
						{
							this._listRight.add(new qx.ui.form.ListItem(structure[r].value, structure[r].icon, structure[r].value));
						}
					}
					else
					{
						if (structure[r].label != null)
						{
							this._listRight.add(new qx.ui.form.ListItem(structure[r].label, structure[r].icon, structure[r].label));
						}
					}
				}
				else {
					if (structure[r].value != null)
					{
						if (structure[r].label != null)
						{
							this._listLeft.add(new qx.ui.form.ListItem(structure[r].label, structure[r].icon, structure[r].value));
						}
						else
						{
							this._listLeft.add(new qx.ui.form.ListItem(structure[r].value, structure[r].icon, structure[r].value));
						}
					}
					else
					{
						if (structure[r].label != null)
						{
							this._listLeft.add(new qx.ui.form.ListItem(structure[r].label, structure[r].icon, structure[r].label));
						}
					}
				}
			}
			
			var layerBtns = new qx.ui.container.Composite((new qx.ui.layout.VBox(5)).set({alignY:"middle"}));
				layerBtns.add(this._btnToRight);
				layerBtns.add(this._btnToLeft);
			
			this._btnToRight.addListener("execute", function(){
				if (!(this._listLeft.isSelectionEmpty())){
					this._listRight.add(this._listLeft.getSelectedItem());
				}
			},this);
			this._btnToLeft.addListener("execute", function(){
				if (!(this._listRight.isSelectionEmpty())){
					this._listLeft.add(this._listRight.getSelectedItem());
				}
			},this);
			
			//ADD
			this.add(new qx.ui.basic.Label(labelLeft), {row: 0, column: 0});
			this.add(new qx.ui.basic.Label(labelRight), {row: 0, column: 2})
			this.add(this._listLeft, {row: 1, column: 0});
			this.add(layerBtns, {row: 1, column: 1});
			this.add(this._listRight, {row: 1, column: 2});
			
			
			
	},
/*
*****************************************************************************
     MEMBERS
*****************************************************************************
*/
	members : {
		_listLeft: null,
		_listRight: null,
		_btnToLeft: null,
		_btnToRight: null,

		/**
		* Devuelve un *array* con los valores del *List Seleccionados*.
		* @return {String[]}
		*/
		getValues : function () { 
			 var items = this._listRight.getChildren(); 
			 var vector = [];
			 for (r in items){
				vector.push(items[r].getValue());
			 }
			 return vector;
		},

		/**
		* Devuelve un *Array* con los textos mostrados en el *List Seleccionados*.
		* @return {String[]}
		*/
		getValuesDisplay : function () { 
			 var items = this._listRight.getChildren(); 
			 var vector = [];
			 for (r in items){
				vector.push(items[r].getLabel());
			 }
			 return vector;
		},
		focus : function ()
		{
			this._listLeft.select(this._listLeft.getChildren()[0]);
		}
	}
});
